package common;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.io.File;

import bean.OAuthInfo;
import bean.wraper.DefaultIntWrapper;
import bean.wraper.DefaultStringWrapper;
import bean.wraper.FavArticleListWrapper;
import bean.wraper.FavNewsListWrapper;
import bean.wraper.HotKeyWordWraper;
import bean.wraper.TopicWrapper;
import bean.wraper.NewsTabMapWrapper;
import bean.wraper.TabArticleListWrapper;
import bean.wraper.TabNewsListWrapper;
import bean.wraper.TabTopicListWrapper;
import bean.wraper.UserInfoWrapper;
import share.LoginType;

public class Requester {

    public static AsyncHttpClient addAdvice(final String content, final String contacInfo, final HttpCallBack<DefaultStringWrapper> callBack) {
        try {
            RequestParams params = new RequestParams();
            params.put("content", content);
            params.put("contact", contacInfo);
            params.put("gt_clientid", UserConfig.getGetuiId());
            String url = ServerConfig.API_HOST + "/addadvice";
            return HttpHelper.executePost(DefaultStringWrapper.class, url, params, callBack);
        } catch (Exception e) {
            e.printStackTrace();
            return new AsyncHttpClient();
        }
    }

    /**
     * 登录
     */
    public static AsyncHttpClient bind(LoginType loginType, String openId, String nickname, String iconUrl, String token, final HttpCallBack<DefaultIntWrapper> callBack) {
        String type = null;
        switch (loginType) {
            case QQ:
                type = "qq";
                break;
            case SINA:
                type = "sinawb";
                break;
            case WECHAT:
                type = "weixin";
                break;
        }
        RequestParams params = new RequestParams();
        params.put("type", type);
        params.put("external_uid", openId);
        params.put("external_name", nickname);
        params.put("avatar", iconUrl);
        params.put("token", token);
        String url = ServerConfig.API_HOST + "/userinfo/bind";
        return HttpHelper.executePost(DefaultIntWrapper.class, url, params, callBack);
    }

    public static AsyncHttpClient bindGetui(String id, final HttpCallBack<DefaultIntWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/userinfo/getui";
        RequestParams params = new RequestParams();
        params.put("clientid", id);
        return HttpHelper.executePost(DefaultIntWrapper.class, url, params, callBack);
    }

    /**
     * 评论
     *
     * @param callBack
     * @return
     */
    public static AsyncHttpClient comment(final String comment, final String newsId, final String articleId, final String topicId, final String commentId, final String location, final HttpCallBack<DefaultStringWrapper> callBack) {

        RequestParams params = new RequestParams();
        params.put("topic_id", topicId);
        params.put("news_id", newsId);
        params.put("article_id", articleId);
        params.put("content", comment);
        params.put("refer", commentId);
        params.put("location", location);
        params.put("gt_clientid", UserConfig.getGetuiId());
        String url = ServerConfig.API_HOST + "/addcomment";
        return HttpHelper.executePost(DefaultStringWrapper.class, url, params, callBack);
    }

    /**
     * 获取凿音
     */
    public static AsyncHttpClient digNewsByArticle(final int index, final String topicId, final HttpCallBack<TabTopicListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/wa";
        RequestParams params = new RequestParams();
        params.put("times", index + "");
        params.put("article_id", topicId);
        return HttpHelper.executeGet(TabTopicListWrapper.class, url, params, callBack);
    }

    /**
     * 获取凿音
     */
    public static AsyncHttpClient digNewsByNews(final int index, final String newsId, final HttpCallBack<TabTopicListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/wa";
        RequestParams params = new RequestParams();
        params.put("times", index + "");
        params.put("news_id", newsId);
        return HttpHelper.executeGet(TabTopicListWrapper.class, url, params, callBack);
    }

    /**
     * 获取(凿音)专题列表
     *
     */
    public static AsyncHttpClient getCollectionArticleList(final HttpCallBack<FavArticleListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/attention";
        RequestParams params = new RequestParams();
        params.put("type", "article");
        return HttpHelper.executeGet(FavArticleListWrapper.class, url, params, callBack);
    }

    /**
     * 获取(新闻)专题列表
     *
     */
    public static AsyncHttpClient getCollectionNewsList(final HttpCallBack<FavNewsListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/attention";
        RequestParams params = new RequestParams();
        params.put("type", "news");
        return HttpHelper.executeGet(FavNewsListWrapper.class, url, params, callBack);
    }

    /**
     * 获取(凿音)专题列表
     *
     */
    public static AsyncHttpClient getCollectionTopicList(final HttpCallBack<TabTopicListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/attention";
        RequestParams params = new RequestParams();
        params.put("type", "topic");
        return HttpHelper.executeGet(TabTopicListWrapper.class, url, params, callBack);
    }

    /**
     * 获取(新闻)专题列表
     *
     * @param tab
     * @param page
     */
    public static AsyncHttpClient getCurrentTabNewsList(final int tab, final int page, final HttpCallBack<TabNewsListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/list";
        RequestParams params = new RequestParams();
        params.put("page", page + "");
        params.put("tab", tab + "");
        return HttpHelper.executeGet(TabNewsListWrapper.class, url, params, callBack);
    }

    /**
     * 获取(凿音)专题列表
     *
     * @param tab
     * @param page
     */
    public static AsyncHttpClient getDigTabNewsList(final int tab, final int page, final HttpCallBack<TabTopicListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/list";
        RequestParams params = new RequestParams();
        params.put("page", page + "");
        params.put("tab", tab + "");
        return HttpHelper.executeGet(TabTopicListWrapper.class, url, params, callBack);
    }

    /**
     * 获取专题详情
     */
    public static AsyncHttpClient getNewsGroup(final String topicId, final HttpCallBack<TopicWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/topic/" + topicId;
        return HttpHelper.executeGet(TopicWrapper.class, url, callBack);
    }

    /**
     * 获取新闻tab标签
     */
    public static AsyncHttpClient getNewsTabList(final HttpCallBack<NewsTabMapWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/tablist";
        return HttpHelper.executeGet(NewsTabMapWrapper.class, url, null, callBack);
    }

    /**
     * 获取(观点)专题列表
     *
     * @param tab
     * @param page
     */
    public static AsyncHttpClient getPointTabNewsList(final int tab, final int page, final HttpCallBack<TabArticleListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/list";
        RequestParams params = new RequestParams();
        params.put("page", page + "");
        params.put("tab", tab + "");
        return HttpHelper.executeGet(TabArticleListWrapper.class, url, params, callBack);
    }

    /**
     * 获取搜索热词
     */
    public static AsyncHttpClient getSearchHotKeys(final HttpCallBack<HotKeyWordWraper> callBack) {
        String url = ServerConfig.API_HOST + "/search/hotkw";
        return HttpHelper.executeGet(HotKeyWordWraper.class, url, callBack);
    }


    public static AsyncHttpClient getUserInfo(final HttpCallBack<UserInfoWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/userinfo";
        return HttpHelper.executeGet(UserInfoWrapper.class, url, null, callBack);
    }

    /**
     * 登录
     */
    public static AsyncHttpClient login(LoginType loginType, String openId, String nickname, String iconUrl, String token, final HttpCallBack<UserInfoWrapper> callBack) {
        String type = null;
        switch (loginType) {
            case QQ:
                type = OAuthInfo.TYPE_QQ;
                break;
            case SINA:
                type = OAuthInfo.TYPE_WB;
                break;
            case WECHAT:
                type = OAuthInfo.TYPE_WX;
                break;
        }
        RequestParams params = new RequestParams();
        params.put("type", type);
        params.put("external_uid", openId);
        params.put("external_name", nickname);
        params.put("avatar", iconUrl);
        params.put("token", token);
        String url = ServerConfig.API_HOST + "/login";
        return HttpHelper.executePost(UserInfoWrapper.class, url, params, callBack);
    }

    /**
     * 登出
     */
    public static AsyncHttpClient logout(final HttpCallBack<DefaultIntWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/logout";
        return HttpHelper.executePost(DefaultIntWrapper.class, url, callBack);
    }

    /**
     * 关注
     *
     * @param callBack
     * @return
     */
    public static AsyncHttpClient markCareTopic(final String topicId, final HttpCallBack<DefaultStringWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/attention/" + topicId;
        RequestParams params = new RequestParams();
        params.put("type", "topic");
        return HttpHelper.executePost(DefaultStringWrapper.class, url, null, callBack);
    }

    /**
     * 关注
     *
     * @param callBack
     * @return
     */
    public static AsyncHttpClient markCareNews(final String topicId, final HttpCallBack<DefaultStringWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/attention/" + topicId;
        RequestParams params = new RequestParams();
        params.put("type", "news");
        return HttpHelper.executePost(DefaultStringWrapper.class, url, params, callBack);
    }

    /**
     * 关注
     *
     * @param callBack
     * @return
     */
    public static AsyncHttpClient markCareArticle(final String topicId, final HttpCallBack<DefaultStringWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/attention/" + topicId;
        RequestParams params = new RequestParams();
        params.put("type", "article");
        return HttpHelper.executePost(DefaultStringWrapper.class, url, params, callBack);
    }

    public static AsyncHttpClient modifyIcon(final File file, final HttpCallBack<DefaultStringWrapper> callBack) {
        try {
            RequestParams params = new RequestParams();
            params.put("avatar", file);
            String url = ServerConfig.API_HOST + "/userinfo/avatar";
            return HttpHelper.executePost(DefaultStringWrapper.class, url, params, callBack);
        } catch (Exception e) {
            e.printStackTrace();
            return new AsyncHttpClient();
        }
    }

    /**
     * @param callBack
     * @return
     */
    public static AsyncHttpClient modifyName(final String name, final HttpCallBack<DefaultIntWrapper> callBack) {
        RequestParams params = new RequestParams();
        params.put("name", name);
        String url = ServerConfig.API_HOST + "/userinfo/name";
        return HttpHelper.executePost(DefaultIntWrapper.class, url, params, callBack);
    }

    /**
     * 获取专题列表及每个专题的前5条新闻摘要
     *
     * @param keyword
     * @param page
     */
    public static AsyncHttpClient searchNewsGroup(final String keyword, final int page, final HttpCallBack<TabTopicListWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/search/" + keyword;
        RequestParams params = new RequestParams();
        params.put("page", page + "");
        return HttpHelper.executeGet(TabTopicListWrapper.class, url, params, callBack);
    }

    /**
     * @param type     "news" or "topic"
     * @param id
     * @param callBack
     * @return
     */
    public static AsyncHttpClient shareLog(final String type, final String id, final HttpCallBack<DefaultStringWrapper> callBack) {
        try {
            RequestParams params = new RequestParams();
            String url = ServerConfig.API_HOST + "/share";
            params.put("type", type);
            params.put("id", id);
            AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
            asyncHttpClient.get(url, params, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                }

                @Override
                public void onSuccess(int i, Header[] headers, String s) {
                }
            });
            return HttpHelper.executePost(DefaultStringWrapper.class, url, params, callBack);
        } catch (Exception e) {
            e.printStackTrace();
            return new AsyncHttpClient();
        }
    }

    public static AsyncHttpClient unbind(LoginType loginType, final HttpCallBack<DefaultIntWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/userinfo/unbind";
        String type = null;
        switch (loginType) {
            case QQ:
                type = "qq";
                break;
            case SINA:
                type = "sinawb";
                break;
            case WECHAT:
                type = "weixin";
                break;
        }
        RequestParams params = new RequestParams();
        params.put("type", type);
        return HttpHelper.executePost(DefaultIntWrapper.class, url, params, callBack);
    }

    /**
     * 顶评论
     */
    public static AsyncHttpClient vote(final String commentId, final HttpCallBack<DefaultStringWrapper> callBack) {
        String url = ServerConfig.API_HOST + "/votecomment/" + commentId;
        return HttpHelper.executePost(DefaultStringWrapper.class, url, callBack);
    }

}
